package org.bitoy.shimmer.config.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 *  统计接口访问次数
 *  每个请求只执行一次，服务器内部的转发请求不会过滤，自定义filter推荐继承OncePerRequestFilter，不推荐实现Filter接口
 *  WebFilter注解必须与 ServletComponentScan(basePackages = {"org.bitoy.shimmer.config.filter"})一起使用才起作用
 *  Order注解不生效，推荐在配置了中配置顺序
 */
@Slf4j
//@Order(1)
@WebFilter(filterName = "GatewayFilter", urlPatterns = {"/*"})
public class GatewayFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        log.info("接口访问统计过滤器：" + request.getRequestURI());
        filterChain.doFilter(request, response);
    }
}
